# Author: Dustin Dannenhauer
# Contact: dtdannen@indiana.edu

# This file hits the local server
# with a hardcoded query. Its purpose is to
# demonstrate that the server can be hit remotely
# and the caching mechanism that makes repeated queries
# much faster than new ones.

import urllib
import urllib2

# This is the local server
url = 'http://0.0.0.0:8080/'

# This is a hardcoded query to use for now
host1 = "http://dbpedia.org/sparql"
prefix1 = """
        PREFIX owl: <http://www.w3.org/2002/07/owl#>
        PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>
        PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
        PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
        PREFIX foaf: <http://xmlns.com/foaf/0.1/>
        PREFIX dc: <http://purl.org/dc/elements/1.1/>
        PREFIX : <http://dbpedia.org/resource/>
        PREFIX dbpedia2: <http://dbpedia.org/property/>
        PREFIX dbpedia: <http://dbpedia.org/>
        PREFIX skos: <http://www.w3.org/2004/02/skos/core#>
        """
select1 = """ SELECT ?subject ?population """ 
where1 = """  WHERE {
               ?subject rdf:type <http://dbpedia.org/ontology/City>.
               ?subject <http://dbpedia.org/ontology/populationUrban> ?population.
               FILTER (xsd:integer(?population) > 2000000)
               }
        ORDER BY DESC(xsd:integer(?population))
        LIMIT 10
        """
# End hardcoded query

# Query String
qstr = prefix1 + select1 + where1

# Stuff to send to the server
values = {'host' : host1,
          'query' : qstr}

if __name__ == "__main__":
    data = urllib.urlencode(values)
    req = urllib2.Request(url, data)
    response = urllib2.urlopen(req)
    the_page = response.read()
    print the_page

